package com.g4.shoppingsingle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.g4.shoppingsingle.mapper.DdProductInfoMapper;
import com.g4.shoppingsingle.pojo.DdProductInfo;
import com.g4.shoppingsingle.service.IDdProductInfoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 商品表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2021-10-27
 */
@Service
public class DdProductInfoServiceImpl extends ServiceImpl<DdProductInfoMapper, DdProductInfo> implements IDdProductInfoService {

    @Autowired(required = false)
    private DdProductInfoMapper ddProductInfoMapper;

    @Autowired
    private RestHighLevelClient restHighLevelClient;

    @Override
    public PageInfo<DdProductInfo> pdName(Integer pdType, Integer pageNum,Integer pageSize) {

        //只要在执行查询前设置PageHelper分页,这次查询就会自动变为分页查询
        //startPage([页码(1表示第一页)],[每页条数])
        PageHelper.startPage(pageNum,pageSize);
        //select * from dd_product_info limit 1,10;
        QueryWrapper<DdProductInfo> wrapper = new QueryWrapper<>();
//        //select * from dd_product_info where pd_name="小米6" limit 1,10;
        wrapper.eq("category_id",pdType);

        List<DdProductInfo> list = ddProductInfoMapper.selectList(wrapper);
        return new PageInfo<DdProductInfo>(list);
    }


    //搜索
    public List< Object> search(String keyword, Integer pageNo, Integer pageSize) throws Exception {

        //搜索为空
        SearchRequest searchRequest = new SearchRequest("mi_dd_product_info");
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        //分页
        sourceBuilder.from(pageNo);
        sourceBuilder.size(pageSize);
        //模糊匹配
        MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("pdName", keyword);
        sourceBuilder.query(matchQueryBuilder);
        sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
        //执行搜索
        searchRequest.source(sourceBuilder);
        SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        //解析结果
        List<Object> list = new ArrayList<>();
        for (SearchHit hit : searchResponse.getHits().getHits()) {
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            list.add(sourceAsMap);
        }
        return list;
    }

}
